<?php

use Phalcon\Paginator\Adapter\Model as PaginatorModel;

class AreaController extends ControllerBase
{
    public function initialize()
    {
        parent::initialize();
    }

    public function arealistAction()
    {
        //当前页数
        if (!isset($this->params['page']) || empty($this->params['page'])) {
            $this->params['page'] = 1;
        }
        //当前条数
        if (!isset($this->params['limit']) || empty($this->params['limit'])) {
            $this->params['limit'] = 10;
        }
        $cityColorManagerDb     = new CityColorManager();
        $getlist['items']       = $cityColorManagerDb->get_list($this->params);
        $getlist['total_items'] = $cityColorManagerDb->get_list($this->params, 'count');
        if (!empty($getlist['items'])) {
            $city_order = new CityOrder();
            $user       = new User();
            foreach ($getlist['items'] as $key => $val) {
                $getlist['items'][$key]['money']      = 0;
                $getlist['items'][$key]['count']      = 0;
                $getlist['items'][$key]['user_count'] = 0;
                if ($val['user_id']) {
                    $orderCount['conditions']        = "user_id = {$val['user_id']} and tk_earning_time is not null and order_status in(4,6)";
                    $orderCount['columns']           = "sum(money) as money";
                    $city_orderinfo                  = $city_order->findFirst($orderCount);
                    if($city_orderinfo) {
                        $getlist['items'][$key]['money'] = $city_orderinfo->money;
                    }
                    $orderCount['columns']           = "count(id) as count";
                    $city_orderinfo                  = $city_order->findFirst($orderCount);
                    if($city_orderinfo) {
                        $getlist['items'][$key]['count'] = $city_orderinfo->count;
                    }
                    $orderCounts['conditions']             = "user_id = {$val['user_id']} ";
                    $orderCounts['columns']                = "id";
                    $orderCounts['group']                  = "user_id";
                    $userInfo                             = $city_order->find($orderCounts)->toarray();
                    if(!empty($userInfo)){
                        $getlist['items'][$key]['user_count'] = count($userInfo);
                    }
                }

                //区域人数
                $area_ids = $cityColorManagerDb->down_area_ids($val['path']);
                $area_people['conditions'] = "area_id in($area_ids)";
                $getlist['items'][$key]['area_user_count'] = $user->count($area_people);
            }
        }
        $this->ok($getlist);
    }


    public function pid_area_codeAction()
    {
        //当前页数
        if (!isset($this->params['pid_area_code']) || empty($this->params['pid_area_code'])) {
            $this->params['pid_area_code'] = '';
        }
        $cityColorManagerDb = new CityColorManager();
        $get_area_code      = $cityColorManagerDb->get_area_code($this->params['pid_area_code']);
        $this->ok($get_area_code);
    }

    /**
     * 修改价格
     */
    public function price_city_color_managerAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, '缺少ID');
        }
        if (!isset($this->params['old_price']) || empty($this->params['old_price'])) {
            $this->fail(-1, '缺少原价');
        }
        if (!isset($this->params['end_price']) || empty($this->params['end_price'])) {
            $this->fail(-1, '缺少折后价');
        }
        $city_color_manager = new CityColorManager();
        $city_color_manager = $city_color_manager->findFirstById($this->params['id']);
        if ($city_color_manager) {
            $city_color_manager->old_price = $this->params['old_price'];
            $city_color_manager->end_price = $this->params['end_price'];
            $city_color_manager->save();
            $this->ok();
        }
        $this->fail(-1, '不存在!');
    }

    public function getcityOrderAction()
    {
        $city_order = new CityOrder();
        $orderCount['columns']           = "sum(money) as money";
        $city_orderinfo                  = $city_order->findFirst($orderCount);
        if($city_orderinfo){
            $result['yugu_money'] = $city_orderinfo->money;
        } else {
            $result['yugu_money'] = 0;
        }
        $city_order = new CityOrder();
        $orderCount['conditions']        = "tk_earning_time is not null and order_status in(4,6)";
        $orderCount['columns']           = "sum(money) as money";
        $city_orderinfo                  = $city_order->findFirst($orderCount);
        if($city_orderinfo){
            $result['money'] = $city_orderinfo->money;
        } else {
            $result['money'] = 0;
        }

        $orderCount['columns']           = "count(id) as count";
        $city_orderinfo                  = $city_order->findFirst($orderCount);
        if($city_orderinfo){
            $result['count'] = $city_orderinfo->count;
        } else {
            $result['count'] = 0;
        }
        $this->ok([$result]);
    }

    public function add_city_color_managerAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, '缺少ID');
        }
        if (!isset($this->params['phone']) || empty($this->params['phone'])) {
            $this->fail(-1, '缺少手机号');
        }
        if (!isset($this->params['end_time']) || empty($this->params['end_time'])) {
            $this->fail(-1, '缺少结束时间');
        }
        $user     = new User();
        $userInfo = $user->findFirstByPhone($this->params['phone']);
        if (!$userInfo) {
            $this->fail(-1, '该手机号未注册!');
        }
        $city_color_manager = new CityColorManager();

        $userCity = $city_color_manager->findFirstByUserId($userInfo->id);
        if ($userCity && strtotime($userCity->end_time) > strtotime($this->params['end_time'])) {
            $this->fail(-1, '该手机号已是区代!');
        }
        $city_color_manager = $city_color_manager->findFirstById($this->params['id']);
        if (!empty($city_color_manager->end_time) && strtotime($city_color_manager->end_time) > strtotime($this->params['end_time'])) {
            $this->fail(-1, '该区域未结束!');
        }
        $city_color_manager->user_id   = $userInfo->id;
        $city_color_manager->user_name = $userInfo->real_name;
        $city_color_manager->end_time  = $this->params['end_time'];
        if ($city_color_manager->save()) {
            $this->ok('操作成功!');
        }
        $userInfo->is_city_area_manager = 1;
        $userInfo->save();

        $this->fail(-1, '保存失败!');
    }
}
